<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- 搜索条件 -->
      <el-form-item>
        <el-date-picker v-model="queryParams.tradeDate" size="small" value-format="yyyy-MM-dd" type="month" placeholder="选择月" @change="getList">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-tag style="margin-right: 5px;cursor:pointer"
                v-for="tag in tags"
                :key="tag.name"
                :type="tag.type" @click="chooseTag(tag.name)"
        >
          {{ tag.name }}
        </el-tag>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['eye:stockRiseFallNum:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格列表 -->
    <el-table id="tableDiv" :height="tableHeight" v-loading="loading" border :summary-method="getSummaries" show-summary :data="stockRiseFallNumList"
              @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="交易日期" align="center" width="100">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.tradeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="成交额" align="center" width="80">
        <template v-slot="scope">
          <span>{{ scope.row.turnover }}亿</span>
        </template>
      </el-table-column>
      <el-table-column label="大盘净流入" width="100" align="center" prop="shSzNetInflow">
        <template v-slot="scope">
          <span style="color: red" v-if="scope.row.shSzNetInflow > 0">+{{ scope.row.shSzNetInflow }}亿</span>
          <span style="color: green" v-else>
            {{ scope.row.shSzNetInflow }}亿
          </span>
        </template>
      </el-table-column>
      <el-table-column label="沪深股通净流入" width="115" align="center" prop="hkNetInflow">
        <template v-slot="scope">
          <span style="color: red" v-if="scope.row.hkNetInflow > 0">+{{ scope.row.hkNetInflow }}亿</span>
          <span style="color: green" v-else>
            {{ scope.row.hkNetInflow }}亿
          </span>
        </template>
      </el-table-column>
      <el-table-column label="涨跌比" width="160" align="center">
        <template v-slot="scope">
          <el-progress :text-inside="true" :stroke-width="20" text-color="white" color="#ff3c3c" define-back-color="#428F1C"
                       :percentage="parseInt(scope.row.riseNum/(scope.row.riseNum+scope.row.fallNum)*100)"
          ></el-progress>
        </template>
      </el-table-column>
      <el-table-column label="上涨数" align="center">
        <template v-slot="scope">
          <span style="color: red">{{ scope.row.riseNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下跌数" align="center">
        <template v-slot="scope">
          <span style="color: green">{{ scope.row.fallNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="涨停数" align="center">
        <template v-slot="scope">
          <span style="color: red">{{ scope.row.riseStopNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="跌停数" align="center">
        <template v-slot="scope">
          <span style="color: green">{{ scope.row.fallStopNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['eye:stockRiseFallNum:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['eye:stockRiseFallNum:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页-->
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改股票涨跌数对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="交易日期" prop="tradeDate">
          <el-date-picker clearable v-model="form.tradeDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择交易日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="成交额" prop="turnover">
          <el-input v-model="form.turnover" placeholder="请输入成交额"/>
        </el-form-item>
        <el-form-item label="大盘净流入" prop="shSzNetInflow">
          <el-input v-model="form.shSzNetInflow" placeholder="大盘净流入"/>
        </el-form-item>
        <el-form-item label="沪深股通净流入" prop="hkNetInflow">
          <el-input v-model="form.hkNetInflow" placeholder="请输入沪深股通净流入(单位亿)"/>
        </el-form-item>
        <el-form-item label="上涨数" prop="riseNum">
          <el-input v-model="form.riseNum" placeholder="请输入上涨数"/>
        </el-form-item>
        <el-form-item label="下跌数" prop="fallNum">
          <el-input v-model="form.fallNum" placeholder="请输入下跌数"/>
        </el-form-item>
        <el-form-item label="涨停数" prop="riseStopNum">
          <el-input v-model="form.riseStopNum" placeholder="请输入涨停数"/>
        </el-form-item>
        <el-form-item label="跌停数" prop="fallStopNum">
          <el-input v-model="form.fallStopNum" placeholder="请输入跌停数"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStockRiseFallNum,
  getStockRiseFallNum,
  delStockRiseFallNum,
  addStockRiseFallNum,
  updateStockRiseFallNum
} from '@/api/eye/stockRiseFallNum'
import dayjs from 'dayjs'

export default {
  name: 'StockRiseFallNum',
  data() {
    return {
      tags: [],
      tableHeight: 450,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 股票涨跌数表格数据
      stockRiseFallNumList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        stockCode: null,
        tradeDate: null,
        shSzNetInflow: null,
        hkNetInflow: null,
        riseNum: null,
        fallNum: null,
        riseStopNum: null,
        fallStopNum: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  updated() {
    this.getTableMaxHeight()
  },
  created() {
    this.queryParams.tradeDate = dayjs().startOf('month').format('YYYY-MM-DD')
    let type = ['', 'success', 'info', 'warning', 'danger','','warning','danger']
    for (let i = 0; i < 8; i++) {
      this.tags.push({ name: dayjs().add(-i, 'month').startOf('month').format('YYYY-MM'), type: type[i] })
    }
    this.getList()
  },
  methods: {
    chooseTag(e) {
      this.queryParams.tradeDate = e + '-01'
      console.log(this.queryParams.tradeDate)
      this.getList()
    },
    //获取容器当前高度，重设表格的最大高度
    getTableMaxHeight() {
      this.$nextTick(function() {
        let t = document.querySelector('#tableDiv')
        console.log('涨跌 重置高度')
        if (t == null) return
        this.tableHeight = window.innerHeight - t.getBoundingClientRect().top - 69
      })
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总价'
          return
        }
        if (index === 3 || index === 4) {
          const values = data.map(item => Number(item[column.property]))
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] = sums[index].toFixed(2) + ' 亿'
        } else {
          sums[index] = 'N/A'
        }
      })
      return sums
    },
    /** 查询股票涨跌数列表 */
    getList() {
      this.loading = true
      listStockRiseFallNum(this.queryParams).then(response => {
        this.stockRiseFallNumList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stockCode: null,
        tradeDate: null,
        shSzNetInflow: null,
        hkNetInflow: null,
        riseNum: null,
        fallNum: null,
        riseStopNum: null,
        fallStopNum: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加股票涨跌数'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getStockRiseFallNum(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改股票涨跌数'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStockRiseFallNum(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addStockRiseFallNum(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除股票涨跌数编号为"' + ids + '"的数据项？').then(function() {
        return delStockRiseFallNum(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }
  }
}
</script>
<style scoped>
::v-deep .el-table__footer-wrapper tbody td.el-table_1_column_4 {
  border-radius: 15px;
  background-color: #fde2e2;
  color: red;
}

::v-deep .el-table__footer-wrapper tbody td.el-table_1_column_5 {
  border-radius: 15px;
  background-color: #fde2e2;
  color: red;
}
</style>
